using System;
using DevExpress.Data.Filtering;
using DevExpress.Xpo;
using SmartPeople.Framework.SecuritySpace;

namespace SmartPeople.Framework.Security.Providers.XPO
{
    [Persistent("SecuritySecuredDocument")]
    internal class XPSecuritySecuredDocument : XPCustomObject, ISecuritySecuredDocument
    {
        public XPSecuritySecuredDocument(Session session) : base(session)
        {
            // This constructor is used when an object is loaded from a persistent storage.
            // Do not place any code here.
        }


        public XPSecuritySecuredDocument()
        {
        }

        #region Old Constructor

        //public XPSecuritySecuredDocument(string name, ISecuritySecuredType securedType,
        //                                 XPSecuritySecuredDocument parentSecuredDocument)
        //{
        //    //gID = Guid.NewGuid();
        //    this.name = name;
        //    this.securedType = (XPSecuritySecuredType) securedType;
        //    this.parentSecuredDocument = parentSecuredDocument;
        //} 

        #endregion

        #region Fields

        [Key(AutoGenerate = true)]
        [Persistent("GID")]
        private Guid gID;

        [Persistent("Name")]
        private string name;

        [Persistent("Description")]
        private string description;

        [Persistent("SecuredType"), Association("SecuredType", typeof (XPSecuritySecuredType))]
        private XPSecuritySecuredType securedType;

        [Persistent("DefaultChildSecuredType"), Association("DefaultChildSecuredType", typeof (XPSecuritySecuredType))]
        private XPSecuritySecuredType defaultChildSecuredType;

        [Persistent("ParentSecuredDocument"), Association("ParentSecuredDocument", typeof (XPSecuritySecuredDocument))]
        private XPSecuritySecuredDocument parentSecuredDocument;

        #endregion

        #region Properties

        [NonPersistent]
        public Guid GID
        {
            get { return gID; }
            set
            {
                if (gID != value)
                {
                    Guid OldValue = gID;
                    gID = value;
                    OnChanged("GID", OldValue, value);
                }
            }
        }

        [NonPersistent]
        public string Name
        {
            get { return name; }
            set
            {
                if (name != value)
                {
                    string OldValue = name;
                    name = value;
                    OnChanged("Name", OldValue, value);
                }
            }
        }

        [NonPersistent]
        public string Description
        {
            get { return description; }
            set
            {
                if (description != value)
                {
                    string OldValue = description;
                    description = value;
                    OnChanged("Description", OldValue, value);
                }
            }
        }

        [NonPersistent]
        public ISecuritySecuredType SecuredType
        {
            get
            {
                if (securedType == null)
                    return parentSecuredDocument.DefaultChildSecuredType;
                else
                    return securedType;
            }
            set
            {
                if (securedType != value)
                {
                    ISecuritySecuredType OldValue = securedType;
                    securedType = (XPSecuritySecuredType) value;
                    OnChanged("SecuredType", OldValue, value);
                }
            }
        }

        [NonPersistent]
        public ISecuritySecuredType DefaultChildSecuredType
        {
            get { return defaultChildSecuredType; }
            set
            {
                if (defaultChildSecuredType != value)
                {
                    ISecuritySecuredType OldValue = defaultChildSecuredType;
                    defaultChildSecuredType = (XPSecuritySecuredType) value;
                    OnChanged("DefaultChildSecuredType", OldValue, value);
                }
            }
        }


        [NonPersistent]
        public string FullName
        {
            get
            {
                string fullName = name;
                if (parentSecuredDocument != null)
                    fullName = parentSecuredDocument.FullName + "/" + fullName;

                fullName = fullName.Replace("//", "/");
                return fullName;
            }
        }

        //was internal before
        [NonPersistent]
        public XPSecuritySecuredDocument ParentSecuredDocument
        {
            get { return parentSecuredDocument; }
            set
            {
                if (parentSecuredDocument != value)
                {
                    XPSecuritySecuredDocument OldValue = parentSecuredDocument;
                    parentSecuredDocument = value;
                    OnChanged("ParentSecuredDocument", OldValue, value);
                }
            }
        }

        #endregion

        #region GetActionID

        //Try to find action name / if action name not found return full control
        public Guid GetActionID(string actionName)
        {
            if (String.IsNullOrEmpty(actionName))
                return BuiltInActions.FullControl;

            if (securedType == null)
                return BuiltInActions.FullControl;

            GroupOperator criteria = new GroupOperator();
            criteria.Operands.Add(new BinaryOperator("Name", actionName, BinaryOperatorType.Equal));
            XPCollection<XPSecuritySecuredTypeAction> xpCollection =
                new XPCollection<XPSecuritySecuredTypeAction>(securedType.actions, criteria);
            if (xpCollection.Count == 1)
            {
                XPSecuritySecuredTypeAction SecuritySecuredTypeAction = xpCollection[0];
                return SecuritySecuredTypeAction.GID;
            }

            return BuiltInActions.FullControl;
        }

        #endregion

        #region GetStateID

        public Guid GetStateID(string stateName)
        {
            if (String.IsNullOrEmpty(stateName))
                return BuiltInStates.Any;

            if (securedType == null)
                return BuiltInStates.Any;

            GroupOperator criteria = new GroupOperator();
            criteria.Operands.Add(new BinaryOperator("Name", stateName, BinaryOperatorType.Equal));
            XPCollection<XPSecuritySecuredTypeState> xpCollection =
                new XPCollection<XPSecuritySecuredTypeState>(securedType.states, criteria);
            if (xpCollection.Count == 1)
            {
                XPSecuritySecuredTypeState SecuritySecuredTypeState = xpCollection[0];
                return SecuritySecuredTypeState.GID;
            }

            return BuiltInStates.Any;
        }

        #endregion
    }
}